package com.sinopec.basemodule.camera.utils;

import android.content.Context;
import android.os.Environment;

import androidx.annotation.Nullable;

import com.sinopec.basemodule.camera.model.CameraConfig;
import com.sinopec.basemodule.common.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 类描述：相机工具类<br/>
 * 创建人：吴冬冬<br/>
 * 创建时间：2019/4/3 16:56 <br/>
 */
public class CameraUtils {
    /**
     * 生成文件夹目录
     */
    public static File generateStorageDir(Context context, @Nullable String pathToDirectory) {
        File mediaStorageDir = null;
        if (pathToDirectory != null) {
            mediaStorageDir = new File(pathToDirectory);
        } else {
            mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), context.getPackageName());
        }

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Logger.e("123===", "创建文件夹失败");
                return null;
            }
        }

        return mediaStorageDir;
    }

    /**
     * 获取输出文件
     */
    public static File getOutputMediaFile(Context context, @CameraConfig.MediaAction int mediaAction, @Nullable String pathToDirectory, @Nullable String fileName) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        final File mediaStorageDir = generateStorageDir(context, pathToDirectory);
        File mediaFile = null;

        if (mediaStorageDir != null) {
            if (fileName == null) {
                final String timeStamp = simpleDateFormat.format(new Date());
                if (mediaAction == CameraConfig.MEDIA_ACTION_PHOTO) {
                    fileName = "IMG_" + timeStamp;
                } else if (mediaAction == CameraConfig.MEDIA_ACTION_VIDEO) {
                    fileName = "VID_" + timeStamp;
                }

            }
            final String mediaStorageDirPath = mediaStorageDir.getPath();
            if (mediaAction == CameraConfig.MEDIA_ACTION_PHOTO) {
                mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".jpg");
            } else if (mediaAction == CameraConfig.MEDIA_ACTION_VIDEO) {
                mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".mp4");
            }
        }

        return mediaFile;
    }

}
